#include<stdio.h>
#define FILEINP "DIADAO.INP"
#define FILEOUT "DIADAO.OUT"
int n,k,m;
int c[2][101][101];
int visit[2][101][101];
int flag=0;
void input(){
    FILE *fp=fopen(FILEINP,"r");
    fscanf(fp,"%d%d%d",&n,&m,&k);
    int i,u,v,p;
    for(i=1;i<=k;i++){
        fscanf(fp,"%d%d%d",&u,&v,&p);
        if(p==0)p=-1;
        c[0][u][v]=c[1][u][v]=p;
    }
    fclose(fp);
}
int val(int x,int y){
    if(x<0 || y<0)return 0;
    if(x>=n || y>=m)return 0;
    return visit[flag][x][y]==0;
}
int turnLeft(int &dx,int &dy){
    if(dx==0){
        dx=dy;
        dy=0;
    }else{
        dy=-dx;
        dx=0;
    }
}
int turnRight(int &dx,int &dy){
    if(dx==0){
        dx=-dy;
        dy=0;
    }else{
        dy=dx;
        dx=0;
    }
}
int get(int _dx,int _dy){
    int dx=_dx,dy=_dy;
    int result=0;
    int cx=0,cy=0;
    do{
        result++;
        cx+=dx;cy+=dy;
        if(!val(cx,cy))return result;
        visit[flag][cx][cy]=1;
        if(c[flag][cx][cy]==-1)turnLeft(dx,dy);
        else if(c[flag][cx][cy]==1)turnRight(dx,dy);
    }while(1);
    
}
int main(){
    input();
    int result1,result2;
    result1=get(1,0);
    flag=1;
    result2=get(0,1);
    FILE *fp=fopen(FILEOUT,"w");
    if(result1<result2)fprintf(fp,"%d %d",result1,result2);
    else fprintf(fp,"%d %d",result2,result1);
    fclose(fp);
    return 0;
}
        
        
        

